<!-- HTML header for doxygen 1.8.3.1-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.3.1"/>
<title>AngelScript: Garbage collected objects</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script type="test/javascript" src="touch.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.3.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('doc_gc_object.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Pages</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Garbage collected objects </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Reference counting as memory management has a drawback in that it is difficult to detect circular references when determining dead objects. AngelScript allows the application to register types with special behaviours to support the garbage collection for detecting circular references. These behaviours make the class a bit more complex, but you should only have to register them for a few types, e.g. generic container classes.</p>
<div class="fragment"><div class="line"><span class="comment">// Registering the garbage collected reference type</span></div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#a29c6c087c8c5b5cdb6271cfd161cc5a6" title="Registers a new object type.">RegisterObjectType</a>(<span class="stringliteral">&quot;ref&quot;</span>, 0, <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aa9450e038342b36c745858d2e5ae4b861" title="A reference type.">asOBJ_REF</a> | <a class="code" href="angelscript_8h.html#a855d86fa9ee15b9f75e553ee376b5c7aacc1d835f9c25043cef86026a4aa6a470" title="A garbage collected type. Only valid for reference types.">asOBJ_GC</a>); assert( r &gt;= 0 );</div>
</div><!-- fragment --><p>The difference between the garbage collected and non-garbage collected types is in the addref and release behaviours, the class constructor, and the extra support behaviours.</p>
<dl class="section see"><dt>See Also</dt><dd>The <a class="el" href="doc_addon_dict.html">dictionary</a> add-on for an example of a garbage collected object</dd></dl>
<h1><a class="anchor" id="doc_reg_gcref_1"></a>
GC support behaviours</h1>
<p>The GC determines when objects should be destroyed by counting the references it can follow for each object. If the GC can see all references that points to an object, it knows that the object is part of a circular reference. If all the objects involved in that circular reference have no outside references it means that they should be destroyed.</p>
<p>The process of determining the dead objects uses the first four of the behaviours below, while the destruction of the objects is done by forcing the release of the object's references.</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> CGCRef::SetGCFlag()</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// Set the gc flag as the high bit in the reference counter</span></div>
<div class="line">    refCount |= 0x80000000;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">bool</span> CGCRef::GetGCFlag()</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// Return the gc flag</span></div>
<div class="line">    <span class="keywordflow">return</span> (refCount &amp; 0x80000000) ? <span class="keyword">true</span> : <span class="keyword">false</span>;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">int</span> CGCRef::GetRefCount()</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// Return the reference count, without the gc flag</span></div>
<div class="line">    <span class="keywordflow">return</span> (refCount &amp; 0x7FFFFFFF);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> CGCRef::EnumReferences(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine)</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// Call the engine::GCEnumCallback for all references to other objects held</span></div>
<div class="line">    engine-&gt;<a class="code" href="classas_i_script_engine.html#a58ceeafd780dea3543e0ede4106199fd" title="Used by the garbage collector to enumerate all references held by an object.">GCEnumCallback</a>(myref);</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> CGCRef::ReleaseAllReferences(<a class="code" href="classas_i_script_engine.html" title="The engine interface.">asIScriptEngine</a> *engine)</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// When we receive this call, we are as good as dead, but</span></div>
<div class="line">    <span class="comment">// the garbage collector will still hold a references to us, so we</span></div>
<div class="line">    <span class="comment">// cannot just delete ourself yet. Just free all references to other</span></div>
<div class="line">    <span class="comment">// objects that we hold</span></div>
<div class="line">    <span class="keywordflow">if</span>( myref )</div>
<div class="line">    {</div>
<div class="line">        myref-&gt;Release();</div>
<div class="line">        myref = 0;</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">// Register the GC support behaviours</span></div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad69bc821a7f1120369c1bd9526f41b9c" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5aadbad474a338c3a0fe6e90df679bb2e6" title="(GC) Set GC flag">asBEHAVE_SETGCFLAG</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,SetGCFlag), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );</div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad69bc821a7f1120369c1bd9526f41b9c" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5abfce2539609e667f15b24bbc8551c7b7" title="(GC) Get GC flag">asBEHAVE_GETGCFLAG</a>, <span class="stringliteral">&quot;bool f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,GetGCFlag), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );</div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad69bc821a7f1120369c1bd9526f41b9c" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5af998529f8ea1e54567997b8fb2867640" title="(GC) Get reference count">asBEHAVE_GETREFCOUNT</a>, <span class="stringliteral">&quot;int f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,GetRefCount), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );</div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad69bc821a7f1120369c1bd9526f41b9c" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a08ccf78a37567b5dd192ff5d95c6667b" title="(GC) Enumerate held references">asBEHAVE_ENUMREFS</a>, <span class="stringliteral">&quot;void f(int&amp;in)&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,EnumReferences), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );</div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad69bc821a7f1120369c1bd9526f41b9c" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a4275ebe0b4852f2d4a10d4d9db333fe9" title="(GC) Release all references">asBEHAVE_RELEASEREFS</a>, <span class="stringliteral">&quot;void f(int&amp;in)&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,ReleaseAllReferences), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_reg_gcref_2"></a>
Factory for garbage collection</h1>
<p>Whenever a garbage collected class is created, the garbage collector must be notified of it's existence. The easiest way of doing that is to have the factory behaviour, or the class constructor call the <code>NotifyGarbageCollectorOfNewObject()</code> method on the engine when initializing the class.</p>
<div class="fragment"><div class="line">CGCRef *GCRef_Factory()</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// Create the object and then notify the GC of its existence</span></div>
<div class="line">    CGCRef *obj = <span class="keyword">new</span> CGCRef();</div>
<div class="line">    <a class="code" href="classas_i_type_info.html" title="The interface for describing types This interface is used to describe the types in the script engine...">asITypeInfo</a> *type = engine-&gt;<a class="code" href="classas_i_script_engine.html#aea2583b8aa724779b7c37df8b7fa437b" title="Returns the type interface by name.">GetTypeInfoByName</a>(<span class="stringliteral">&quot;gc&quot;</span>);</div>
<div class="line">    engine-&gt;<a class="code" href="classas_i_script_engine.html#a52a7644b48cbc771e33db5070814f6df" title="Notify the garbage collector of a new object that needs to be managed.">NotifyGarbageCollectorOfNewObject</a>(obj, type);</div>
<div class="line">    <span class="keywordflow">return</span> obj;</div>
<div class="line">}</div>
</div><!-- fragment --><p>You may want to consider caching the typeId, so that it doesn't have to be looked up through the relatively expensive call to GetTypeIdByDecl every time an object of this type is created.</p>
<p>Note, if you create objects of this type from the application side, you must also notify the garbage collector of its existence, so it's a good idea to make sure all code use the same way of creating objects of this type.</p>
<h1><a class="anchor" id="doc_reg_gcref_3"></a>
Addref and release for garbage collection</h1>
<p>For garbage collected objects it is important to make sure the AddRef and Release behaviours clear the GC flag. Otherwise it is possible that the GC incorrectly determine that the object should be destroyed.</p>
<div class="fragment"><div class="line"><span class="keywordtype">void</span> CGCRef::AddRef()</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// Clear the gc flag and increase the reference counter</span></div>
<div class="line">    refCount = (refCount&amp;0x7FFFFFFF) + 1;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="keywordtype">void</span> CGCRef::Release()</div>
<div class="line">{</div>
<div class="line">    <span class="comment">// Clear the gc flag, decrease ref count and delete if it reaches 0</span></div>
<div class="line">    refCount &amp;= 0x7FFFFFFF;</div>
<div class="line">    <span class="keywordflow">if</span>( --refCount == 0 )</div>
<div class="line">        <span class="keyword">delete</span> <span class="keyword">this</span>;</div>
<div class="line">}</div>
<div class="line"></div>
<div class="line"><span class="comment">// Registering the addref/release behaviours</span></div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad69bc821a7f1120369c1bd9526f41b9c" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a1dfa5b72ad69a7bf70636d4fcb1b1d84" title="AddRef.">asBEHAVE_ADDREF</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,AddRef), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );</div>
<div class="line">r = engine-&gt;<a class="code" href="classas_i_script_engine.html#ad69bc821a7f1120369c1bd9526f41b9c" title="Registers a behaviour for the object type.">RegisterObjectBehaviour</a>(<span class="stringliteral">&quot;gc&quot;</span>, <a class="code" href="angelscript_8h.html#a7e38df5b10ec8cbf2a688f1d114097c5a7134ce13c81967191af401a1e5170a0c" title="Release.">asBEHAVE_RELEASE</a>, <span class="stringliteral">&quot;void f()&quot;</span>, <a class="code" href="angelscript_8h.html#a7345e6b3afabec24efd0ff77886d49a6" title="Returns an asSFuncPtr representing the class method specified by class and method name...">asMETHOD</a>(CGCRef,Release), <a class="code" href="angelscript_8h.html#a3ec92ea3c4762e44c2df788ceccdd1e4aea516c8742acc1edff6a43dc1bb09e96" title="A thiscall class method.">asCALL_THISCALL</a>); assert( r &gt;= 0 );</div>
</div><!-- fragment --><h1><a class="anchor" id="doc_reg_gcref_4"></a>
Garbage collected objects and multi-threading</h1>
<p>If you plan on executing scripts from multiple threads with <a class="el" href="doc_adv_custom_options.html#doc_adv_custom_options_engine">automatic garbage collection</a> turned on, or if you plan on running the garbage collector manually from a background thread, then you must make sure that the object type behaviours that support the garbage collector are thread-safe. Especially the ADDREF, RELEASE, and ENUMREFS behaviours have a high probability of being invoked from multiple threads simultaneously. The RELEASEREFS behaviour will only be invoked when the Garbage Collector has determined that the object is already dead so it is guaranteed not to be invoked by multiple threads. The others, GETREFCOUNT, SETGCFLAG, and GETGCFLAG, are not sensitive as the garbage collector just use the information as a hint.</p>
<p>Making the ADDREF and RELEASE behaviours thread-safe is easy with the use of <a class="el" href="angelscript_8h.html#af0074d581ac2edd06e63e56e4be52c8e">asAtomicInc</a> and <a class="el" href="angelscript_8h.html#a0565bcb53be170dd85ae27a5b6f2b828">asAtomicDec</a>. If the object is static container, i.e. the memory layout of the contents cannot change, then ENUMREFS is already thread-safe, but if memory layout can change, e.g. dynamic arrays or hash maps, then the iteration over the content in ENUMREFS must be protected so that it doesn't break in case the memory happen to change in the middle of the iteration.</p>
<dl class="section see"><dt>See Also</dt><dd><a class="el" href="doc_gc.html#doc_gc_threads">Garbage collection and multi-threading</a> </dd></dl>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Sun Dec 18 2016 12:35:28 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.3.1 </li>
  </ul>
</div>
</body>
</html>
